package com.dropTables.TakingNote;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpComm {
	private static String username;
	private static String password;
	private static Boolean loggedIn = false;
	
	public static String executeHttpGet() throws Exception {
		BufferedReader in = null;
		String responseStr = "";
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet();
			request.setURI(new URI("http://sinisterduke.info/SDD/?action=check&username="+username+"&password="+password));
			HttpResponse response = client.execute(request);
			in = new BufferedReader
			(new InputStreamReader(response.getEntity().getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			
			responseStr = sb.toString();
			return responseStr;
		} 
		finally 
		{
			if (in != null) 
			{
				try 
				{
					in.close();
					return responseStr;
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static String executeHttpNearBy(String nB, String loc) throws Exception
	{
		BufferedReader in = null;
		String responseStr = "";
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet();
			request.setURI(new URI("http://sinisterduke.info/SDD/?action=genloc&username="+username+"&password="+password+"&gengps="+loc+"&genloc="+nB) );
			HttpResponse response = client.execute(request);
			in = new BufferedReader
			(new InputStreamReader(response.getEntity().getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			
			responseStr = sb.toString();
			return responseStr;
		} 
		finally 
		{
			if (in != null) 
			{
				try 
				{
					in.close();
					return responseStr;
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static String executeHttpSpecAddr(String addr) throws Exception
	{
		BufferedReader in = null;
		String responseStr = "";
		try
		{
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet();
			request.setURI(new URI("http://sinisterduke.info/SDD/?action=specloc&username="+username+"&password="+password+"&specloc="+addr));
			HttpResponse response = client.execute(request);
			in = new BufferedReader
			(new InputStreamReader(response.getEntity().getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) 
			{
				sb.append(line + NL);
			}
			in.close();
					
			responseStr = sb.toString();
			return responseStr;
		}
		finally
		{
			if(in != null)
			{
				try
				{
					in.close();
					return responseStr;
				}
				catch(IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}
	public static String executeHttpRemove(int id) throws Exception {
		BufferedReader in = null;
		String responseStr = "";
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet();
			request.setURI(new URI("http://sinisterduke.info/SDD/?action=remove&username="+username+"&password="+password+"&reminderid="+id));
			HttpResponse response = client.execute(request);
			in = new BufferedReader
			(new InputStreamReader(response.getEntity().getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			
			responseStr = sb.toString();
			return responseStr;
		} 
		finally 
		{
			if (in != null) 
			{
				try 
				{
					in.close();
					return responseStr;
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static String executeHttpAdd(String text, String body,String timestamp,String g_loc,String s_loc) throws Exception {
		BufferedReader in = null;
		String responseStr = "";
		timestamp = timestamp.replaceAll(" ","%20");
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet();
			request.setURI(new URI("http://sinisterduke.info/SDD/?action=add&username="+username+"&password="+password+"&title="+text+"&reminder="+body+"&remindertime="+timestamp+"&genloc="+g_loc+"&specloc="+s_loc));
			HttpResponse response = client.execute(request);
			in = new BufferedReader
			(new InputStreamReader(response.getEntity().getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			
			responseStr = sb.toString();
			return responseStr;
		} 
		finally 
		{
			if (in != null) 
			{
				try 
				{
					in.close();
					return responseStr;
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static String executeHttpEdit(Long id, String text, String body,String timestamp,String g_loc,String s_loc) throws Exception {
		BufferedReader in = null;
		String responseStr = "";
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet();
			request.setURI(new URI("http://sinisterduke.info/SDD/?action=edit&reminderid="+id+"&username="+username+"&password="+password+"&title="+text+"&reminder="+body+"&remindertime="+timestamp+"&genloc="+g_loc+"&specloc="+s_loc));
			HttpResponse response = client.execute(request);
			in = new BufferedReader
			(new InputStreamReader(response.getEntity().getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			
			responseStr = sb.toString();
			return responseStr;
		} 
		finally 
		{
			if (in != null) 
			{
				try 
				{
					in.close();
					return responseStr;
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static String executeHttpregister(String uname, String pword,String email) throws Exception {
		BufferedReader in = null;
		String responseStr = "";
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet();
			request.setURI(new URI("http://sinisterduke.info/SDD/?action=register&username="+uname+"&password="+pword+"&email="+email));
			HttpResponse response = client.execute(request);
			in = new BufferedReader
			(new InputStreamReader(response.getEntity().getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line);
			}
			in.close();
			
			responseStr = sb.toString();
			return responseStr;
		} 
		finally 
		{
			if (in != null) 
			{
				try 
				{
					in.close();
					return responseStr;
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static String executeHttplogin(String uname, String pword) throws Exception{
		BufferedReader in = null;
		String responseStr = "";
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet();
			request.setURI(new URI("http://sinisterduke.info/SDD/?action=login&username="+uname+"&password="+pword));
			HttpResponse response = client.execute(request);
			in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while((line = in.readLine()) != null){
				sb.append(line + NL);
			}
			in.close();
			
			responseStr = sb.toString();
			return responseStr;
		}
		finally
		{
			if (in != null)
			{
				try
				{
					in.close();
					return responseStr;
				}
				catch(IOException e){
					e.printStackTrace();
				}
			}
		}
	}
	
	
	public static void readLoginInfo(){
        try 
        {
                FileInputStream is = new FileInputStream("/data/data/com.dropTables.TakingNote/files/TakingNotelogininfo");
                DataInputStream in = new DataInputStream(is);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                username = br.readLine();
                password = br.readLine();
        } 
        catch (FileNotFoundException e) {
                e.printStackTrace();
        } 
        catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
        if(username == null || password == null){
                username = null;
                password = null;
        }
        else{
                loggedIn = true;
        }
	}

	public static String getLoginInfo(){
		if (loggedIn)
                return username + " " + password;
        return "";
	}
	
	public static boolean isLoggedIn()
	{
		return loggedIn;
	}
	public static boolean logout()
	{
		File f = new File("/data/data/com.dropTables.TakingNote/files/TakingNotelogininfo");
		if(!f.exists())
		{
			System.out.println("No login file found");
		}
		if(!f.delete())
		{
			System.out.println("Couldn't delete login file.");
			return false;
		}
		if(loggedIn)
		{
			
			loggedIn = false;
			username = null;
			password = null;
		}
		return true;
	}
	
}
